package com.xywy.bloodandroid.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.act.DoctorDetailFrameAct;
import com.xywy.bloodandroid.base.BasicAdapter;
import com.xywy.bloodandroid.bean.HotDoctorInfo;
import com.xywy.bloodandroid.util.ImageLoader.ImageCallback;

public class HotDoctotAdapter extends BasicAdapter<HotDoctorInfo> {

	private LayoutInflater inflater;

	public HotDoctotAdapter(Context context, List<HotDoctorInfo> list) {
		super(context, list);
		this.inflater = LayoutInflater.from(context);

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		viewholder holder;
		if (convertView == null) {
			holder = new viewholder();
			convertView = inflater.inflate(R.layout.item_hot_doctor, null);
			holder.head=(ImageView) convertView.findViewById(R.id.head_img);
			holder.name=(TextView) convertView.findViewById(R.id.name);
			convertView.setTag(holder);
		} else {
			holder = (viewholder) convertView.getTag();
		}
		
		String head_url=list.get(position).getApp_image();
		
		String name=list.get(position).getName();
		
		Drawable cachedImage = loader.loadDrawable(head_url,
				holder.head, new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						if (imageView != null && imageDrawable != null) {
							imageView.setBackgroundDrawable(imageDrawable);
						}
					}
				});
    	
    	if(cachedImage==null){
    		holder.head.setBackgroundResource(R.drawable.head);
    	}else{
    		holder.head.setBackgroundDrawable(cachedImage);
    	}
    	
    	holder.name.setText(name);
    	
    	convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
             Intent intent=new Intent(context, DoctorDetailFrameAct.class);
             Bundle bundle = new Bundle();
             bundle.putSerializable("HotDoctorInfo", list.get(position));
             intent.putExtras(bundle);
//             intent.putExtra("HotDoctorInfo", list.get(position));
             context.startActivity(intent);
			}
		});
		
		return convertView;
	}

	private class viewholder {
		private ImageView head;
		private TextView name;
	}
}
